package facades;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import org.apache.commons.beanutils.locale.LocaleBeanUtils;

import com.db4o.ext.ExtObjectContainer;
import com.db4o.servlet.Db4oServlet;
 
public class ApplicationWebImp {
	private PageContext context=null;
	private String formBeanClass=null;
	private String formAction=null;
	private ExtObjectContainer container = null; 
	private Map errors = new Hashtable();
	
	public void setContext(Object context){
		this.context = (PageContext)context;
		container = (ExtObjectContainer)Db4oServlet.sharedTransaction(
				this.context.getServletContext(),
				this.context.getSession(),
				"padova"
			);
	}
	
	public void setFormBean(String formBeanClass){
		this.formBeanClass = formBeanClass; 
	}
	
	public void setFormAction(String action) throws Exception{
		this.formAction=action;
		setForm();
	}
	
	private void setForm() throws Exception{
		if(formAction.equals("add")) setAddAction();
		if(formAction.equals("edit")) setEditAction();
		if(formAction.equals("save")) setSaveAction();
	}
	
	private void setAddAction() throws Exception{
		try {
			Object bean = Class.forName(formBeanClass).newInstance();
			context.removeAttribute(formBeanClass,PageContext.SESSION_SCOPE);
			context.setAttribute(formBeanClass,bean,PageContext.SESSION_SCOPE);
			context.setAttribute("bean",bean,PageContext.REQUEST_SCOPE);
		} catch (InstantiationException e) {
			System.err.println(e.getLocalizedMessage());
			throw e; 
		}
	}
	
	private void setEditAction()throws Exception{
		context.removeAttribute(formBeanClass,PageContext.SESSION_SCOPE);
		
		try {
			long id = new Long(context.getRequest().getParameter("Id"));
			Object bean = container.getByID(id);
			context.setAttribute(formBeanClass,bean,PageContext.SESSION_SCOPE);
			context.setAttribute("bean",bean,PageContext.REQUEST_SCOPE);
		} catch (Exception e) {
			System.err.println(e.getMessage());
			throw e;
		}
		
		 
	}
	
	private void setSaveAction()throws Exception{
		
		try {
			Object bean = context.getAttribute(formBeanClass,PageContext.SESSION_SCOPE); 
			context.setAttribute("bean",bean,PageContext.REQUEST_SCOPE);
		} catch (Exception e) {
			System.err.println(e.getMessage());
			throw e;
		}
		
		 
	}
	
	public void setPopulate(Map values){
		try {
			if(values==null) values = context.getRequest().getParameterMap();
			Object bean = context.getSession().getAttribute(formBeanClass);
			LocaleBeanUtils.populate(bean,values);
			 
		} catch (Exception e) {
			
		}
	}
	
	public void setSaveFormBean(Map values)throws Exception{
		
		try {
			String errorForm=(String)context.getAttribute("formError",context.REQUEST_SCOPE) ;
			if(values==null) values = context.getRequest().getParameterMap();
			Object bean = context.getSession().getAttribute(formBeanClass);
			LocaleBeanUtils.populate(bean,values);
			
			if(errorForm==null || errorForm.equals("")){	
				container.set(bean);
				context.getSession().removeAttribute(formBeanClass);
				forwardSuccess();
			}else{
				context.setAttribute("bean",bean,context.REQUEST_SCOPE);
				forwardError();
			}
		} catch (Exception e) {
			System.err.println(e.getLocalizedMessage());
			throw e;
		}
	}
	
	private void forwardSuccess() throws ServletException, IOException{
		HttpServletResponse response = (HttpServletResponse)context.getResponse();
		String fwd = (String)context.getAttribute("forwardSuccess",context.REQUEST_SCOPE);
		if(fwd==null || fwd.equals("")) return;
		response.sendRedirect(fwd);
	}
	private void forwardError() throws ServletException, IOException{
		String fwd = (String)context.getAttribute("forwardError",context.REQUEST_SCOPE);
		if(fwd==null || fwd.equals("")) return;
		context.forward(fwd);
	}
	
	public Object getBean(){
		return context.getAttribute(formBeanClass,context.SESSION_SCOPE);
	}
	
	public void setForwardSuccess(String fwd){
		context.setAttribute("forwardSuccess",fwd,context.REQUEST_SCOPE);
	}
	
	public void setForwardError(String fwd){
		context.setAttribute("forwardError",fwd,context.REQUEST_SCOPE);
	}
	
	public String getForward(){
		if(getErrors().size()>0) 
			return (String)context.getAttribute("forwardError");
		else
			return (String)context.getAttribute("forwardSuccess");
	}
	
	public Map getErrors(){
		return errors;
	}
}
